package com.jinsi.service.impl;

import com.jinsi.mapper.HomePageMapper;
import com.jinsi.pojo.ScHomePage;
import com.jinsi.pojo.ScUser;
import com.jinsi.pojo.ScUserLoginLog;
import com.jinsi.service.ScHomePageServer;
import com.jinsi.utils.MapReturn;
import com.jinsi.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Date : 2022/7/7 19:32
 * 小南不难
 **/
@Service
public class ScHomePageServerImpl implements ScHomePageServer {

    @Autowired
    private HomePageMapper homePageMapper;


    //查询今日收款总额，今日加入人数，
    @Override
    public Map<String,Object> getTodayPriceAndCount(){
        try {
            return MapReturn.getSuccess(homePageMapper.getTodayPriceAndCount());
        }catch (Exception e){
            System.out.println("数据库访问出错========>" +e.getMessage());
            return MapReturn.getError("数据访问失败");
        }
    }

    @Override
    public Map<String,Object>  getJoinAndPre() {
        try {
            ScHomePage scHomePage =   homePageMapper.getJoinAndPre();
            return MapReturn.getSuccess(scHomePage);
        }catch (Exception e){
            System.out.println("数据库访问出错========>" +e.getMessage());
            return MapReturn.getError("数据访问失败");
        }
    }

    @Override
    public Map<String, Object> getOneMouthPrice() {
        try {
            return MapReturn.getSuccess(homePageMapper.getOneMouthPrice());
        }catch (Exception e){
            System.out.println("数据库访问出错========>" +e.getMessage());
            return MapReturn.getError("数据访问失败");
        }
    }

    @Override
    public Map<String, Object> getPriceWay() {
        try {
            return  MapReturn.getSuccess(homePageMapper.getPriceWay());
        }catch (Exception e){
            System.out.println("数据库访问出错========>" +e.getMessage());
            return  MapReturn.getError("数据访问失败");
        }
    }

    @Override
    public Map<String,Object> getLoginLog() {
        try {
            return  MapReturn.getSuccess(homePageMapper.getLoginLog());
        }catch (Exception e){
            System.out.println("数据库访问出错========>" +e.getMessage());
            return  MapReturn.getError("数据访问失败");
        }
    }

    @Override
    public Map<String,Object> delLoginLog(int id) {
        try {
            return  MapReturn.getSuccess(homePageMapper.delLoginLog(id));
        }catch (Exception e){
            System.out.println("数据库访问出错========>" +e.getMessage());
            return  MapReturn.getError("删除失败");
        }
    }

    @Override
    public Map<String, Object> uptUserInfo(ScUser scUser) {
        try {
            return  MapReturn.getSuccess(homePageMapper.uptUserInfo(scUser));
        }catch (Exception e){
            System.out.println("数据库访问出错========>" +e.getMessage());
            return  MapReturn.getError("删除失败");
        }
    }

    @Override
    public Map<String, Object> uptUserpwd(String oldPwd,String newPwd,int userId) {
        try {
            ScUser scUser = homePageMapper.getUserpwd(userId); //获取到了账号和pwd
            System.out.println(scUser);
            ScUser sc1 = new ScUser();
            sc1.setUserAccount(scUser.getUserAccount());
            sc1.setUserPwd(oldPwd);
            System.out.println("sc1"+sc1);
            String md5Pwd = Md5Utils.getMd5Pwd(sc1); //解析的旧密码
            if(scUser.getUserPwd().equals(md5Pwd)){
                sc1.setUserPwd(newPwd);
                String newp = Md5Utils.getMd5Pwd(sc1);
                //密码相等，更新密码
                int i = homePageMapper.uptUserpwd(newp, userId);
                if (i == 1){
                    //修改成功
                    return MapReturn.getSuccess(i);
                }else {
                    return MapReturn.getCustom("500","修改异常","null");
                }
            }else {
                return MapReturn.getCustom("500","旧密码不正确","null");
            }
        }catch (Exception e){
            System.out.println("数据库访问出错========>" +e.getMessage());
            return  MapReturn.getError("删除失败");
        }
    }
}
